package com.wxtt.android.view;

import android.support.design.widget.Snackbar;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.fast.dev.frame.utils.UIUtils;
import com.wxtt.android.BaseApplication;


/**
 * 说明：View提示的工具类
 * <p/>
 * 作者：fanly
 * <p/>
 * 时间：2015-8-26
 * <p/>
 * 版本：verson 1.0
 */

public class ViewInject {

    private static ViewInject inject;
    private Toast mToast;
    private Snackbar mSnackbar;

    /*禁止实例化*/
    private ViewInject() {
    }

    public static ViewInject get() {
        if (inject == null) {
            synchronized (ViewInject.class) {
                if (inject == null) {
                    inject = new ViewInject();
                }
            }
        }
        return inject;
    }

    /**
     * 说明：显示短Toast
     *
     * @param msg
     */
    public Toast shortToast(String msg) {
        return showToast(msg, Toast.LENGTH_SHORT);
    }


    /**
     * 说明：显示短Toast
     *
     * @param msg
     */
    public Toast shortToast(int msg) {
        return shortToast(UIUtils.getString(msg));
    }

    /**
     * 说明：显示Toast
     * @param msg
     * @param time
     * @return
     */
    private Toast showToast(String msg, int time) {
        if (mToast == null) {
            mToast = Toast.makeText(BaseApplication.getContext(), msg, time);
        } else {
            mToast.setText(msg);
            mToast.setDuration(time);
        }
        mToast.show();
        return mToast;
    }

    /**
     * 说明：取消显示Toast
     */
    public void cancelToast() {
        if (mToast != null) {
            mToast.cancel();
        }
    }

    /**
     * 说明：显示长Toast
     *
     * @param msg
     */
    public Toast longToast(String msg) {
        return showToast(msg, Toast.LENGTH_LONG);
    }

    /**
     * 说明：显示长Toast
     *
     * @param msg
     */
    public Toast longToast(int msg) {
        return longToast(UIUtils.getString(msg));
    }

    /**
     * 说明：显示Snackbar
     * @param view
     * @param msg 显示内容
     * @param action 点击内容
     * @param bgcolor 背景颜色
     * @param textcolor 文字颜色
     * @param actioncolor 点击文字颜色
     * @param type 类型【长，短】
     * @param listener 点击事件监听
     */
    private void showSnackbar(View view, String msg, String action, int bgcolor, int textcolor, int actioncolor, int type, View.OnClickListener listener) {
        if (mSnackbar == null){
            mSnackbar = Snackbar.make(view, msg, type);
        }
        View barView = mSnackbar.getView();
        if (actioncolor != 0) {
            mSnackbar.setActionTextColor(actioncolor);
        }
        if (textcolor != 0) {
            TextView tv_message = (TextView) barView.findViewById(android.support.design.R.id.snackbar_text);
            tv_message.setTextColor(textcolor);
        }
        mSnackbar.setAction(action, listener);
        mSnackbar.setText(msg);
        barView.setBackgroundColor(bgcolor);
        mSnackbar.show();
    }

    /**
     * 说明：短Snackbar
     * @param view
     * @param msg
     * @param bgcolor
     * @param textcolor
     */
    public void shortSnackbar(View view, String msg, int bgcolor, int textcolor) {
        showSnackbar(view, msg, "", bgcolor, textcolor, 0, Snackbar.LENGTH_SHORT, null);
    }

    /**
     * 说明：长Snackbar
     * @param view
     * @param msg
     * @param bgcolor
     * @param textcolor
     */
    public void longSnackbar(View view, String msg, int bgcolor, int textcolor) {
        showSnackbar(view, msg, "", bgcolor, textcolor, 0, Snackbar.LENGTH_LONG, null);
    }

    /**
     * 说明：总是显示Snackbar
     * @param view
     * @param msg
     * @param action
     * @param bgcolor
     * @param textcolor
     * @param listener
     */
    public void alwaysSnackbar(View view, String msg, String action, int bgcolor, int textcolor, View.OnClickListener listener) {
        showSnackbar(view, msg, action, bgcolor, textcolor, 0, Snackbar.LENGTH_INDEFINITE, listener);
    }

    /**
     * 说明：带action的长Snackbar
     * @param view
     * @param msg
     * @param action
     * @param bgcolor
     * @param textcolor
     * @param listener
     */
    public void longSnackbar(View view, String msg, String action, int bgcolor, int textcolor, View.OnClickListener listener) {
        showSnackbar(view, msg, action, bgcolor, textcolor, 0, Snackbar.LENGTH_LONG, listener);
    }

    /**
     * 说明：带action的短Snackbar
     * @param view
     * @param msg
     * @param action
     * @param bgcolor
     * @param textcolor
     * @param listener
     */
    public void shortSnackbar(View view, String msg, String action, int bgcolor, int textcolor, View.OnClickListener listener) {
        showSnackbar(view, msg, action, bgcolor, textcolor, 0, Snackbar.LENGTH_SHORT, listener);
    }

    /**
     * 说明：隐藏Snackbar
     */
    public void dismissSnackbar(){
        if (mSnackbar != null){
            mSnackbar.dismiss();
        }
    }
}

